<?php
// include 'common.php';
include 'header.php';
// include 'menu.php';
?>

<style>
    .list input {
        border: none;
        font-size: 15px;
    }

    .list .head th {
        border-bottom: 2px solid #eee;
    }

    .list tbody td {
        border-bottom: 1px solid #eee;
    }


    .list .content {
        height: 50px;
    }

</style>


<script>


    function send(url, data) {
        var http;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            http = new XMLHttpRequest();
        } else {
            // IE6, IE5 浏览器执行代码
            http = new ActiveXObject("Microsoft.XMLHTTP");
        }
        http.open('post', url, true);
        http.send(data);

        http.onreadystatechange = function () {
            // console.log(http.readyState + ' ' + http.status);
            //readyState是当前状态
            //0: 请求未初始化
            //1: 服务器连接已建立
            //2: 请求已接收
            //3: 请求处理中
            //4: 请求已完成，且响应已就绪

            //status：http响应码

            if (http.readyState == 4 && http.status == 200) {
                console.log(http.responseText);
            }

        }

    }

    function save(obj) {
        let data = "";
        let btn = obj.parentNode.parentNode;
        btn.childNodes.forEach(function (k, v) {
            let child = k.childNodes[0];
            if (child != undefined && child.nodeName == "LABEL")
                data += 'id=' + child.innerText + "&";

            if (child != undefined && child.type != "checkbox" &&
                child.value != undefined) {
                data += child.name + '=' + child.value + "&";
            }
        })
        data += 'opt=modify';
        send('../usr/plugins/BooksLine/book.php', data);
    }


    function del(obj) {
        let id = 'id=' + obj.parentNode.parentNode.childNodes[3].childNodes[0].innerText;
        let data = id + '&opt=del'
    }


</script>

<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>


<?php
include_once 'SQL.php';
$prefix = $db->getPrefix();
//$sql = str_ireplace('%_prefix_%',$prefix,SQL::$SQL_SELECT);
$books = $db->fetchAll(
    $db->select()->from($prefix . 'booksline')->order($prefix . 'booksline.id', Typecho_Db::SORT_ASC));

?>

<div class="main" align="center">
    <p>
    <h2>管理书籍</h2></p>
    <p>
    <table class="list">
        <colgroup>
            <col width="50"/>
            <col width="50"/>
            <col width="200"/>
            <col width="150"/>
            <col width="300"/>
            <col width="100"/>
            <col width="200"/>
        </colgroup>

        <thead>
        <tr class="head" style="height: 50px">
            <th>ID</th>
            <th>书籍名称</th>
            <th>书籍作者/出版社</th>
            <th>读后简介</th>
            <th>阅读时间</th>
            <th>操作</th>
        </tr>
        </thead>

        <tbody>
        <?php foreach ($books as $book): ?>
            <tr align="center" class="content">
                <td><label><?php echo $book['id']; ?></label></td>
                <td><label><?php echo $book['name']; ?></label></td>
                <td><label><?php echo $book['author']; ?></label></td>
                <td><label><?php echo $book['info']; ?></label></td>
                <td><label><?php echo $book['date']; ?></label></td>
                <td><a href="<?php echo $request->makeUriByRequest('id=' . $book['id']); ?>">编辑</a>
                </td>
            </tr>
        <?php endforeach; ?>

        </tbody>
    </table>
    </p>
    <hr>

    <?php BooksLine_Plugin::form()->render(); ?>


</div>